package com.sita.tboard.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.persistence.TrendMember;
import com.sita.bike.rest.model.RtResourceNote;
import com.sita.bike.support.GlobalContext;
import com.sita.bike.utils.PersistUtils;
import com.sita.tboard.ui.view.CircleImageView;
import com.sita.tboard.util.DateUtils;
import com.squareup.picasso.Picasso;

import java.util.LinkedList;

/**
 * Created by xiaodong on 16/2/18.
 */
public class RtResNoteListAdapter extends BaseAdapter {
    private LinkedList<RtResourceNote> notes = new LinkedList<RtResourceNote>();
    private Context context;
    private LayoutInflater mInflater;

    public RtResNoteListAdapter(Context mContext) {
        context = mContext;
        mInflater = LayoutInflater.from(context);
    }

    public void setItems(LinkedList<RtResourceNote> notes) {
        this.notes = notes;
        notifyDataSetChanged();
    }

    public void addLastItem(RtResourceNote note) {
        if (!notes.contains(note)) {
            if (!notes.isEmpty()) {
                if (note.noteId.compareTo(notes.getFirst().noteId) == 1) {
                    notes.addFirst(note);
                } else {
                    notes.addLast(note);
                }
            } else {
                notes.add(note);
            }

            notifyDataSetChanged();
        }
    }

    public LinkedList<RtResourceNote> getItems() {
        return notes;
    }

    public void addTopItem(RtResourceNote note) {
        notes.addFirst(note);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return notes.size();
    }

    @Override
    public RtResourceNote getItem(int position) {
        return notes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.rt_resource_note_item, null);

            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.avatar = (CircleImageView) convertView.findViewById(R.id.user_avatar);
            holder.nickName = (TextView) convertView.findViewById(R.id.user_nickname);
            holder.message = (TextView) convertView.findViewById(R.id.message);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        RtResourceNote note = getItem(position);
        holder.time.setText(DateUtils.translateDateTime(note.createTime));

        TrendMember account = PersistUtils.getTrendMember(note.accountId);
        Picasso.with(GlobalContext.getGlobalContext()).load(account.getAvatarUrl()).fit().centerInside()
                .into(holder.avatar);
        holder.nickName.setText(account.getNickname());
        holder.message.setText(note.message);

        return convertView;
    }

    class ViewHolder {
        CircleImageView avatar;
        TextView nickName;
        TextView time;
        TextView message;
    }
}
